package com.android.adapters;

import java.util.ArrayList;

import android.R;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.agenda.EventDetailsActivity;
import com.android.entities.AgendaEntry;
import com.android.entities.Group;

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
	
	private Context mContext;
	private ArrayList<Group> groups;
	
	public MyExpandableListAdapter(Context context, ArrayList<Group> groups) {
		mContext = context;
		this.groups = groups;
	}
	
    public AgendaEntry getChild(int groupPosition, int childPosition) {
    	return groups.get(groupPosition).getChild(childPosition);
    }

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    public int getChildrenCount(int groupPosition) {
        return groups.get(groupPosition).getChildren().size();
    }

    public TextView getGenericView() {
        // Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, 64);

        TextView textView = new TextView(mContext);
        textView.setLayoutParams(lp);
        
        // Center the text vertically
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        
        // Set the text starting position
        textView.setPadding(50, 0, 0, 0);
        return textView;
    }
    
    public LinearLayout getLinearLayoutView() {
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, 64);
        
        LinearLayout linearLayout = new LinearLayout(mContext);
        linearLayout.setLayoutParams(lp);
        linearLayout.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        
    	return linearLayout;
    }

    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
    	LinearLayout linearLayout = getLinearLayoutView();
    	
        TextView textView = getGenericView();
        textView.setText(getChild(groupPosition, childPosition).getName());
        
        textView.setOnClickListener(new OnClickListener() {
        	public void onClick(View arg0) {
        		Intent i = new Intent(mContext, EventDetailsActivity.class);
        		i.putExtra("eventId", getChild(groupPosition, childPosition).getId());
        		mContext.startActivity(i);
        	}
        });
        
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(R.drawable.ic_delete);
        
        linearLayout.addView(imageView);
        linearLayout.addView(textView);
        return linearLayout;
    }

    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition).getName();
    }

    public int getGroupCount() {
        return groups.size();
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
    	
        TextView textView = getGenericView();
        textView.setText(getGroup(groupPosition).toString());
        return textView;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public boolean hasStableIds() {
        return true;
    }
    
    public void insertItem(Group item, int index) {
        groups.add(index, item);
        this.notifyDataSetChanged();
    }
    
}
